﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GheSofa.Models;
using System.Text.RegularExpressions;

namespace GheSofa.Controllers
{
    public class SanPhamController : Controller
    {
        private GheSofaEntities db = new GheSofaEntities();
        int pagesize = 6;

        [ChildActionOnly]
        public ActionResult KhungTimKiem()
        {
            return PartialView();
        }

        public ActionResult DanhMuc(int page = 1)
        {
            string tenloai = "";
            string maloai = Request.QueryString["ma"];
            var sanpham = (from p in db.SanPhams where p.MaLoai == maloai select p).ToList();
            var phanloai = (from pl in db.PhanLoais select pl).ToList();
            List<SanPham> sp = new List<SanPham>();
            foreach (SanPham item in sanpham)
                if (item.MaLoai.Trim() == maloai)
                {
                    sp.Add(item);
                }
            foreach (PhanLoai item in phanloai)
                if (item.MaLoai.Trim() == maloai)
                {
                    tenloai = item.TenLoai;
                    break;
                }
            ViewBag.TotalPages = Math.Ceiling((double)sanpham.Count / pagesize);
            ViewBag.maloai = maloai;
            ViewBag.tenloai = tenloai;
            return View(sp.Skip((page - 1) * pagesize).Take(pagesize));
        }

        public ActionResult ChiTietSanPham()
        {
            var dsSP = (from p in db.SanPhams select p).ToList();
            var pl = (from p in db.PhanLoais select p).ToList();
            var nsx = (from p in db.NhaSanXuats select p).ToList();
            string loai = "";
            string nhasx = "";
            int soluong = 0;
            List<SanPham> chitiet = new List<SanPham>();
            string masp = Request.QueryString["masp"];
            foreach (SanPham item in dsSP)
            {
                if (item.MaSP.Trim() == masp)
                {
                    soluong = item.SoLuong.Value;
                    chitiet.Add(item);
                    foreach (PhanLoai i in pl)
                    {
                        if (item.MaLoai == i.MaLoai)
                        {
                            loai = i.TenLoai;
                            break;
                        }
                    }
                    foreach (NhaSanXuat i in nsx)
                    {
                        if (item.MaNSX == i.MaNSX)
                        {
                            nhasx = i.TenNSX;
                            break;
                        }
                    }
                    break;
                }
            }        
            if (chitiet == null) return HttpNotFound();
            ViewBag.loai = loai;
            ViewBag.nhasx = nhasx;
            ViewBag.soluongcon = soluong;
            return View(chitiet);
        }

        [ChildActionOnly]
        public ActionResult sanphamcungloai(int page = 1)
        {
            string masp = Request.QueryString["masp"];
            var spAll = (from sp in db.SanPhams select sp);
            List<SanPham> sanphamcungloai = new List<SanPham>();
            string loai = "";
            foreach (SanPham item in spAll)
            {
                if (item.MaSP.Trim() == masp)
                    loai = item.MaLoai;
            }

            foreach (SanPham item in spAll)
            {
                if (item.MaLoai == loai && item.MaSP.Trim() != masp)
                    sanphamcungloai.Add(item);
            }
            int _pagesize = 3;
            ViewBag.masp = masp;
            ViewBag.TotalPages = Math.Ceiling((double)sanphamcungloai.Count / _pagesize);
            return PartialView(sanphamcungloai.Skip((page - 1) * _pagesize).Take(_pagesize));
        }

        [ChildActionOnly]
        public ActionResult SanPhamBanChay()
        {
            List<SanPham> sachbanchay = new List<SanPham>();
            //List<spbanchay> sp = new List<spbanchay>();
            var sp = (from c in db.ChiTietHoaDons
                      group c by c.MaSP into nhomsp
                      orderby nhomsp.Sum(item => item.SoLuong)
                      select nhomsp).Take(6).ToList();
            foreach (SanPham item in db.SanPhams)
            {
                foreach(var i in sp)
                    {
                    if (item.MaSP == i.Key)
                    {
                        sachbanchay.Add(item);
                    }
                }
            }
            return PartialView(sachbanchay);
        }

        public ActionResult TimKiem(int page = 1)
        {
            string id = Request.QueryString["chuoitk"];
            id = id.ToLower();
            var sanpham = (from p in db.SanPhams select p).ToList();
            List<SanPham> sp = new List<SanPham>();
            foreach (var item in sanpham)
            {
                if (item.TenSP.ToLower().Contains(id))
                    sp.Add(item);
            }
            ViewBag.Chuoi = id;
            ViewBag.KhongTimThay = "Không tìm thấy";
            ViewBag.TotalPages = Math.Ceiling((double)sp.Count / pagesize);
            return View(sp.Skip((page - 1) * pagesize).Take(pagesize));
        }

        public bool IsNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }

        public ActionResult TimKiemNangCao(int page = 1)
        {
            string loaighe = Request.QueryString["loaighe"];
            string tensp = Request.QueryString["tensanpham"];
            tensp = tensp.ToLower();
            string giatu = Request.QueryString["giatu"];
            string den = Request.QueryString["den"];
            int _giatu = 0;
            int _giaden = 0;
            if (loaighe.Equals("tat ca"))
            {
                loaighe = "";
            }
            if (tensp.Equals("tên sản phẩm"))
            {
                tensp = "";
            }
            if (IsNumber(giatu))
            {
                _giatu = int.Parse(giatu);
            }
            else
                _giatu = 0;

            if (IsNumber(den))
            {
                _giaden = int.Parse(den);
            }
            else
                _giaden = int.MaxValue;
            var sp = (from p in db.SanPhams select p).ToList();
            if (!String.IsNullOrEmpty(loaighe))
            {

                var sp1 = (from s in db.SanPhams
                           join p in db.PhanLoais
                                on s.MaLoai equals p.MaLoai
                           where s.MaLoai == p.MaLoai && p.MaLoai == loaighe
                           select s).ToList();
                sp = sp1;
            }
            if (!String.IsNullOrEmpty(tensp))
            {
                sp = sp.Where(t => t.TenSP.ToString().ToLower().Contains(tensp)).ToList();
            }
            sp = sp.Where(t => (t.Gia >= _giatu && t.Gia <= _giaden)).ToList();
            ViewBag.KhongTimThay = "Không tìm thấy các sản phẩm thỏa điều kiện";
            ViewBag.loaihang = loaighe;
            ViewBag.tensp = tensp;
            ViewBag.giatu = giatu;
            ViewBag.giaden = den;
            ViewBag.TotalPages = Math.Ceiling((double)sp.Count / pagesize);
            return View(sp.Skip((page - 1) * pagesize).Take(pagesize));
        }
    }
}
